package tt.dz.appserver.dao.ext;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.ListOrderedMap;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;

import tt.dz.appserver.dao.base.BasisDao;
import tt.dz.appserver.entity.model.ServiceStation;
import tt.dz.appserver.util.Constants;

@Repository
public class ServiceStationDao extends BasisDao<ServiceStation> {

	
	/** 添加服务网点 */
	public int addServiceStation(ServiceStation serviceStation) {

		return super.save(serviceStation);

	}

	/** 删除服务网点 */
	public int delServiceStation(ServiceStation serviceStation) {

		return super.update(serviceStation);

	}

	/** 更新服务网点 */
	public int updateServiceStation(ServiceStation serviceStation) {

		return super.update(serviceStation);

	}

	/** 查询服务网点 */
	public List<ServiceStation> findServiceStationValid(String servicestationname, String provinceid, String cityid,
			String areaid) {

		Integer validflag = 1;
		ListOrderedMap map = new ListOrderedMap();

		if (servicestationname != null) {
			map.put("service_station_name", servicestationname);
		}
		if (provinceid != null) {
			map.put("province_id", provinceid);
		}
		if (cityid != null) {
			map.put("city_id", cityid);
		}
		if (areaid != null) {
			map.put("area_id", areaid);
		}
		map.put("valid_flag", validflag);

		return super.findListAutomaticOfClass(ServiceStation.class, map, null, null, "service_station_name", 1);

	}
	
	
	@Cacheable(value="serviceStation",key="#serviceType+'pageNo'+#pageNo+'longitude'+#longitude+'latitude'+#latitude")
	public List<Map<String,Object>> findNearServiceStationValid(int serviceType, int pageNo, float longitude, float latitude){
		
		String sql = 
				"SELECT"+
				"	service_station_id,"+
				"	service_type,"+
				"	service_station_name,"+
				"	description,"+
				"	business_time,"+
				"	contact_phone,"+
				"	province_id,"+
				"	city_id,"+
				"	area_id,"+
				"	outlets_addr,"+
				"	create_time,"+
				"	update_time, "+
				"	longitude, "+
				"	latitude, "+
				" round(round( "+
				"		6378.138 * 2 * asin( "+
				"			sqrt( "+
				"				pow( "+
				"					sin( "+
				"						( "+
				"							b.latitude * pi() / 180 - ? * pi() / 180 "+
				"						) / 2 "+
				"					), "+
				"					2 "+
				"				) + cos(b.latitude * pi() / 180) * cos(? * pi() / 180) * pow( "+
				"					sin( "+
				"						( "+
				"							b.longitude * pi() / 180 - ? * pi() / 180 "+
				"						) / 2 "+
				"					), "+
				"					2 "+
				"				) "+
				"			) "+
				"		) * 1000 "+
				"	)/1000,2) AS dis "+
				"FROM "+
				"	as_service_station b "+
				"WHERE "+
				"	b.service_type = ? "+ 
				"	AND b.valid_flag = ? "+ 
				"ORDER BY dis ASC ";
		return super.findDataOfMapBySQL(new StringBuffer(sql), pageNo, Constants.PAGE_SIZE, latitude, latitude, longitude, serviceType, 1);
		
	}
	
	/**
	 * 查询服务店详细
	 * @param serviceStationId
	 * @return
	 */
	public ServiceStation findServiceStationByServiceStationId(String serviceStationId){
		
		ServiceStation serviceStation = new ServiceStation();
		serviceStation.setValid_flag(1);
		serviceStation.setService_station_id(serviceStationId);
		List<ServiceStation> serviceStations = super.findExample(serviceStation);
		if(serviceStations.size()>0){
			return serviceStations.get(0);
		}
		return null;
	}
}
